// handlers/catalog.go
// Provides handlers for templates, presets, and recent history data.

package handlers

import (
	"net/http"
	"time"

	"github.com/gin-gonic/gin"
)

// TemplateDTO represents a library template surfaced on the home page.
type TemplateDTO struct {
	ID          int      `json:"id"`
	Title       string   `json:"title"`
	Description string   `json:"description"`
	Tags        []string `json:"tags"`
	Complexity  string   `json:"complexity"`
}

// ListTemplates returns the template catalog.
func (h *Handler) ListTemplates(c *gin.Context) {
	templates, err := h.service.ListTemplates()

	data := make([]TemplateDTO, 0, len(templates))
	for _, tpl := range templates {
		dto := TemplateDTO{
			ID:          tpl.Template.ID,
			Title:       tpl.Template.Title,
			Description: tpl.Template.Description,
			Complexity:  tpl.Template.ComplexityHint,
		}
		if len(tpl.Tags) > 0 {
			dto.Tags = append(dto.Tags, tpl.Tags...)
		}

		data = append(data, dto)
	}

	result := Result[[]TemplateDTO]{
		Code: http.StatusOK,
		Data: data,
		Msg:  ResponseMessage.SUCCESS,
	}

	if err != nil {
		result.Code = http.StatusInternalServerError
		result.Msg = err.Error()
	}

	c.JSON(result.Code, result)
}

// HistoryEntryDTO represents a recent project update for the home history rail.
type HistoryEntryDTO struct {
	ID      string `json:"id"`
	Title   string `json:"title"`
	Summary string `json:"summary"`
	Date    string `json:"date"`
	Device  string `json:"device"`
	Screens int    `json:"screens"`
	Status  string `json:"status"`
}

// ListRecentHistory surfaces recent project summaries.
func (h *Handler) ListRecentHistory(c *gin.Context) {
	history, err := h.service.ListRecentHistory(10)

	data := make([]HistoryEntryDTO, 0, len(history))
	for _, entry := range history {
		data = append(data, HistoryEntryDTO{
			ID:      entry.ProjectID,
			Title:   entry.ProjectName,
			Summary: entry.Summary,
			Date:    entry.OccurredAt.Format(time.RFC3339),
			Device:  entry.Device,
			Screens: entry.PageCount,
			Status:  entry.Status,
		})
	}

	result := Result[[]HistoryEntryDTO]{
		Code: http.StatusOK,
		Data: data,
		Msg:  ResponseMessage.SUCCESS,
	}

	if err != nil {
		result.Code = http.StatusInternalServerError
		result.Msg = err.Error()
	}

	c.JSON(result.Code, result)
}

// ConfigurationOptionDTO mirrors the preset structure from the Settings UI.
type ConfigurationOptionDTO struct {
	Label       string `json:"label"`
	Description string `json:"description,omitempty"`
}

// ConfigurationGroupDTO groups related preset options.
type ConfigurationGroupDTO struct {
	Title    string                   `json:"title"`
	Subtitle string                   `json:"subtitle"`
	Icon     string                   `json:"icon"`
	Options  []ConfigurationOptionDTO `json:"options"`
}

// ListConfigurationPresets returns preset configuration groups.
func (h *Handler) ListConfigurationPresets(c *gin.Context) {
	groups, err := h.service.ListConfigurationPresets()

	data := make([]ConfigurationGroupDTO, 0, len(groups))
	for _, group := range groups {
		options := make([]ConfigurationOptionDTO, 0, len(group.Options))
		for _, opt := range group.Options {
			options = append(options, ConfigurationOptionDTO{
				Label:       opt.Label,
				Description: opt.Description,
			})
		}

		data = append(data, ConfigurationGroupDTO{
			Title:    group.Title,
			Subtitle: group.Subtitle,
			Icon:     group.Icon,
			Options:  options,
		})
	}

	result := Result[[]ConfigurationGroupDTO]{
		Code: http.StatusOK,
		Data: data,
		Msg:  ResponseMessage.SUCCESS,
	}

	if err != nil {
		result.Code = http.StatusInternalServerError
		result.Msg = err.Error()
	}

	c.JSON(result.Code, result)
}
